package umage.figures;

import static org.jhotdraw.draw.AttributeKeys.FILL_COLOR;
import static org.jhotdraw.draw.AttributeKeys.FONT_BOLD;
import static org.jhotdraw.draw.AttributeKeys.STROKE_COLOR;

import java.io.IOException;

import org.jhotdraw.draw.CreationTool;
import org.jhotdraw.draw.EllipseFigure;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.draw.GraphicalCompositeFigure;
import org.jhotdraw.draw.ListFigure;
import org.jhotdraw.draw.RectangleFigure;
import org.jhotdraw.draw.TextFigure;
import org.jhotdraw.draw.VerticalLayouter;
import org.jhotdraw.geom.Insets2DDouble;
import org.jhotdraw.xml.DOMInput;
import org.jhotdraw.xml.DOMOutput;

@FigureInfo(name = "Use Case Figure", icon = "UseCaseFigure.png", toolbar = "Figures", toolbarPriority = 0, tool = CreationTool.class)
public class UseCaseFigure extends GraphicalCompositeFigure {
	
	TextFigure caseName;
	
	public UseCaseFigure() {
		super(new EllipseFigure());
		
		caseName = new TextFigure("Use Case");
		setupLayout();
	}
	
	protected void setupLayout() {
		setLayouter(new VerticalLayouter());

		RectangleFigure UseCaseName = new RectangleFigure();
		STROKE_COLOR.set(UseCaseName, null);
		UseCaseName.setAttributeEnabled(STROKE_COLOR, false);
		FILL_COLOR.set(UseCaseName, null);
		UseCaseName.setAttributeEnabled(FILL_COLOR, false);
		ListFigure UseCaseCompartment = new ListFigure(UseCaseName);

		add(UseCaseCompartment);

		Insets2DDouble insets = new Insets2DDouble(12, 24, 12, 24);
		LAYOUT_INSETS.set(UseCaseCompartment, insets);
		UseCaseCompartment.setAttributeEnabled(LAYOUT_INSETS, true);
		
		UseCaseCompartment.add(caseName);
		FONT_BOLD.set(caseName, true);
		caseName.setAttributeEnabled(FONT_BOLD, true);
	}
	
	public UseCaseFigure clone() {
		UseCaseFigure that = (UseCaseFigure) super.clone();
		that.caseName = new TextFigure(caseName.getText());
		that.removeAllChildren();
		that.setupLayout();
		return that;
	}

	public void write(DOMOutput out) throws IOException {
		out.writeObject(getPresentationFigure());
		out.addAttribute("caseName", caseName.getText());
	}

	@Override
	public void read(DOMInput in) throws IOException {
		removeAllChildren();
		setPresentationFigure((Figure) in.readObject());
		caseName = new TextFigure(in.getAttribute("caseName", "Use Case"));
		setupLayout();
	}

}
